import axios from 'axios';
const qs = require('qs');
import Util from "../util";
var urlencode = require('urlencode');
export default class Service {
      static async get(url, params) {
            let response;
            try {
                  response = await axios.get(url, params);
                  console.log('------------success-----------');
                  console.log(`${response.status}\n${response.statusText}\n${JSON.stringify(response.data, null, 2)}\n`)
            } catch (e) {
                  console.log('------------error-------------');
                  console.error(e);
                  throw e
            }
            return response.data;

      };
      static async post(url, data) {
            try {
                  let res = await axios.post(url, qs.stringify(data))
                  res = res.data
                  return new Promise((resolve, reject) => {
                        if (res.code === 0) {
                              resolve(res)
                        } else {
                              reject(res)
                        }
                  })
            } catch (err) {
                  // return (e.message)
                  alert('服务器出错')
                  console.log(err)
            }
      };
      static async getJock() {
            let msg;
            try {
                  let response;
                  response = await this.get('http://v.juhe.cn/joke/randJoke.php', {
                        params: {
                              key: '6ccf13caf0d0c6cf11029c744163d44b'
                        },
                  });
                  console.log(response)
                  msg = Util.joke(response);
            } catch (e) {
                  console.error(e);
                  msg = '获取笑话失败';
            }
            return msg;
      }

}

